# http://pyserial.sourceforge.net/shortintro.html
import eeml
import serial
import time
import sys

port = '/dev/ttyUSB0'
port = 'COM4'
# parameters
API_KEY = ''
API_URL = '/api/6001.xml'

run = True
try:
    serial = serial.Serial(port, 9600)
except:
    print "Can't open port: ", sys.exc_info()[0]
    run = False
else:
    try:
        ready = serial.readline()
    except:
        print "Can't read from port: ", sys.exc_info()[0]
        run = False
    else:
        print ready

while run:
    serial.write("G")      # write a string
    readings = serial.readline().strip().split(' ') # the readings are separated by spaces
    print readings
    # readings = '12.5 .61 2 3 4  6 7 8 9.9 10.1'.strip().split(' ') # the readings are separated by spaces
    pac = eeml.Pachube(API_URL, API_KEY)
    # pac.update([eeml.Data(0, int(readings[14],16), unit=eeml.Celsius()), eeml.Data(1, readings[10], unit=eeml.RH())])
    reading = int(readings[14],16) * 4.775
    print reading
    pac.update([eeml.Data(0, reading)])
    if readings[14] != 'FFFF':
        try:
            pac.put()
        except:
            print sys.exc_info()[0]
    time.sleep(10)
    # run = False
